<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\AttractionManagement\Model\AttractionUserTouristInfo;

class AttractionUserTouristInfoRepository extends IRepository
{
    public function __construct(protected readonly AttractionUserTouristInfo $model) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'userId'), static function (Builder $query, $user_id) {
                $query->where('user_id', $user_id);
            })
            ->when(Arr::get($params, 'name'), static function (Builder $query, $name) {
                $query->where('name', 'like', "%{$name}%");
            })
            ->when(Arr::get($params, 'phone'), static function (Builder $query, $phone) {
                $query->where('phone', $phone);
            })
            ->when(Arr::get($params, 'idCard'), static function (Builder $query, $id_card) {
                $query->where('id_card', $id_card);
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->get(['id', 'name', 'phone', 'id_card as idCard']);
    }
}